﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace LocationAwareness
{
    /// <summary>
    /// This class serializes and deserializes,
    /// the settings of the programm and provides
    /// them to the GUI.
    /// </summary>
    static class SerializeSettings
    {
        /// <summary>
        /// Method serializes settings
        /// </summary>
        /// <param name="settings">
        /// Actual settings choosen 
        /// to be serialized.
        /// </param>
        public static void saveSettings(Settings settings)
        {                
            //try
            //{
                //& must be replaced because its a xml entity
                settings.ServiceUrl.Replace("&", "&amp;");
                //String path = Directory.GetCurrentDirectory();
                //path = path + "\\Settings.xml";
                FileStream stream = new FileStream("settings.xml", FileMode.Create);
                XmlSerializer xmlSer = new XmlSerializer(typeof(Settings));
                xmlSer.Serialize(stream, settings);
                stream.Close();
            //}
            //catch (Exception)
            //{
            //    //MessageBox.Show(rm.GetString("EATk_SaveError"));
            //    return;
            //}
        }

        /// <summary>
        /// Method deserializes settings
        /// </summary>
        /// <returns>Settings saved on client plattform </returns>
        public static Settings loadSettings()
        {
            Settings settings;
            //String path = Directory.GetCurrentDirectory();
            //path = path + "\\Settings.xml";

            if (File.Exists("settings.xml"))
            {
                try
                {
                    FileStream fs = new FileStream("settings.xml", FileMode.Open);
                    XmlSerializer xmlSer = new XmlSerializer(typeof(Settings));

                    settings = (Settings)xmlSer.Deserialize(fs);
                    fs.Close();

                    return settings;
                }
                catch (Exception)
                {
                    //MessageBox.Show(rm.GetString("EATk_LoadError"));
                    return null;
                }
            }
            else
                return null;
        }
    }
}
